package com.t249.bdqn.renting.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.t249.bdqn.renting.entity.RetRentalInformation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;
import java.util.Map;

@Component
@Mapper
public interface RetRentalInformationDao extends BaseMapper<RetRentalInformation> {

    //根据城市id来查询数据
    @Select("SELECT * FROM ret_rental_information c,ret_housing_resources h,ret_region r,ret_huxing xi,ret_address a,ret_payment p WHERE c.housing_Id=h.id AND r.`id`=c.`region_Id` AND c.`huxing_Id` = xi.`id` AND c.`address_Id` = a.`id` AND c.`payment_Id`=p.`id` and c.address_Id = #{id}")
    public List<Map<String,Object>> ChengshiSelect(Integer id);

    //根据城市id来查询数据
    @Select("SELECT * FROM ret_rental_information c,ret_housing_resources h,ret_region r,ret_huxing xi,ret_address a,ret_payment p,adm_type tp WHERE c.housing_Id=h.id AND r.`id`=c.`region_Id` AND c.`huxing_Id` = xi.`id` AND c.`address_Id` = a.`id` AND c.`payment_Id`=p.`id` AND c.ret_typeId = tp.`id` AND c.address_Id = #{id} and user_Id = #{userId}")
    public List<Map<String,Object>> ChengshiSelect1(@Param("id")Integer id,@Param("userId")String userId);

    //根据城市id来查询数据
    @Select("SELECT * FROM ret_rental_information c,ret_housing_resources h,ret_region r,ret_huxing xi,ret_address a,ret_payment p,adm_type tp WHERE c.housing_Id=h.id AND r.`id`=c.`region_Id` AND c.`huxing_Id` = xi.`id` AND c.`address_Id` = a.`id` AND c.`payment_Id`=p.`id` AND c.ret_typeId = tp.`id` AND c.ret_id = #{id}")
    public List<Map<String,Object>> ChengshiSelect2(@Param("id")Integer id);

    //根据区域id来查询数据
    @Select("SELECT * FROM ret_rental_information c,ret_housing_resources h,ret_region r,ret_huxing xi,ret_address a,ret_payment p WHERE c.housing_Id=h.id AND r.`id`=c.`region_Id` AND c.`huxing_Id` = xi.`id` AND c.`address_Id` = a.`id` AND c.`payment_Id`=p.`id` and c.region_Id = #{id}")
    public List<Map<String,Object>> ShiquSelect(Integer id);

    //根据房源id来查询数据
    @Select("SELECT * FROM ret_rental_information c,ret_housing_resources h,ret_region r,ret_huxing xi,ret_address a,ret_payment p WHERE c.housing_Id=h.id AND r.`id`=c.`region_Id` AND c.`huxing_Id` = xi.`id` AND c.`address_Id` = a.`id` AND c.`payment_Id`=p.`id`AND c.ret_id = #{id}")
    public List<Map<String,Object>> FanyuanSelect(Integer id);

    @Select("SELECT * FROM ret_rental_information c,ret_housing_resources h,ret_region r,ret_huxing xi,ret_address a,ret_payment p ${ew.customSqlSegment} AND c.housing_Id=h.id AND r.`id`=c.`region_Id` AND c.`huxing_Id` = xi.`id` AND c.`address_Id` = a.`id` AND c.`payment_Id`=p.`id`  GROUP BY c.region_Id")
    public List<Map<String,Object>> test(@Param(Constants.WRAPPER) Wrapper<RetRentalInformation> userWrapper);

    //根据房源id来查询数据
    @Select("SELECT * FROM ret_rental_information c,ret_housing_resources h,ret_region r,ret_huxing xi,ret_address a,ret_payment p WHERE c.housing_Id=h.id AND r.`id`=c.`region_Id` AND c.`huxing_Id` = xi.`id` AND c.`address_Id` = a.`id` AND c.`payment_Id`=p.`id`AND c.lease_out = #{leaseOut}")
    public List<Map<String,Object>> leaseOutSelect(String leaseOut);

    @Select("SELECT * FROM  `ret_rental_information` WHERE address_Id = #{addressId} GROUP BY lease_out ORDER BY (CASE WHEN lease_out LIKE  #{leaseOut}  THEN 1 ELSE 2 END)")
    public List<RetRentalInformation> retRentalInformationList(@Param("addressId")Integer addressId,@Param("leaseOut") String leaseOut);

    @Select("SELECT * FROM `ret_rental_information` r,`ret_region` n WHERE r.`region_Id` = n.`id` AND r.address_Id = #{addressId} AND r.`lease_out` LIKE #{leaseOut}")
    public List<Map<String,Object>> chanquyu(@Param("addressId")Integer addressId,@Param("leaseOut") String leaseOut);

    @Select("SELECT * FROM  `ret_rental_information` WHERE 1=1 GROUP BY lease_out ORDER BY (CASE WHEN ret_propertiesId = #{retPropertiesId} THEN 1 ELSE 2 END)")
    public List<RetRentalInformation> lou(@Param("retPropertiesId")String retPropertiesId);

    @Select("SELECT sum(1) FROM ret_rental_information ${ew.customSqlSegment}")
    public Integer selectByMyWrapper(@Param(Constants.WRAPPER) Wrapper<RetRentalInformation> userWrapper);

    @Select("SELECT * FROM ret_rental_information ${ew.customSqlSegment}")
    public List<RetRentalInformation> Wrapper(@Param(Constants.WRAPPER) Wrapper<RetRentalInformation> userWrapper);

    @Select("SELECT MAX(ret_id) FROM ret_rental_information")
    public int MaxId();

    @Update("UPDATE `renting`.`ret_rental_information` SET `ret_picture` = #{retPicture} WHERE `ret_id` = #{retId}")
    public int updateFanyuan(String retPicture,int retId);

    @Update("UPDATE `renting`.`ret_rental_information` SET `money` = #{money},`payment_Id` = #{paymentId},`housing_Id` = #{housingId},`ret_describe` = #{retDescribe},`ret_jiaoton` = #{retJiaoton},`FAQISHIJIAN` = #{shijiang} WHERE `ret_id` =#{retId} ")
    public int updatetijiao(@Param("money")String money,@Param("paymentId")String paymentId,@Param("housingId")String housingId,@Param("retDescribe")String retDescribe,@Param("retJiaoton")String retJiaoton,@Param("shijiang")String shijiang,@Param("retId")String retId);

    @Select("SELECT * FROM `ret_rental_information` c,`sys_booking` b,`ret_properties` r ,adm_type t,`ret_huxing` h WHERE  c.`huxing_Id` =  h.id AND  c.`ret_id` = b.`rental_id` AND t.`id` = b.`rental_stateid` AND c.ret_propertiesId = r.id AND c.address_Id = #{sid}  AND c.`user_Id`= #{userId}")
    public List<Map<String,Object>> shujuchaxun(@Param("sid")Integer sid, @Param("userId")String userId);

}
